{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KZ88G-iWCTs7"
      },
      "source": [
        "基於 4chan 魔改版 NovelAILeaks (naifu) 製作。 [來源](https://boards.4channel.org/g/thread/89095460#p89097704)\n",
        "\n",
        "使用官方前端 + 優化版後端，可突破75限制，支持所有模型。"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nGwPS1cKX3U7"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "X5yF8TS1CR3L"
      },
      "outputs": [],
      "source": [
        "#@title ### 0. 檢查 GPU 工作狀態\n",
        "\n",
        "!nvidia-smi"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iqTO_Uf3F6VW"
      },
      "outputs": [],
      "source": [
        "#@title ### 1. 下載 Novel AI API 後端、模型 \n",
        "#@markdown 如果下載速度太慢可以嘗試 restart\n",
        "\n",
        "%cd /content/\n",
        "!apt install -y -qq aria2\n",
        "!aria2c --summary-interval=5 -x 3 --allow-overwrite=true -Z \\\n",
        "  https://pub-2fdef7a2969f43289c42ac5ae3412fd4.r2.dev/naifu.tar \\\n",
        "  https://pub-2fdef7a2969f43289c42ac5ae3412fd4.r2.dev/animefull-latest.tar \n",
        "  \n",
        "!echo \"Decompressing...\"\n",
        "!tar xf naifu.tar && rm naifu.tar\n",
        "!echo \"Done.\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "BysBfYRmGSo1"
      },
      "outputs": [],
      "source": [
        "#@title ### 2. 安裝依賴\n",
        "#@markdown 耐心等待安裝完成\n",
        "\n",
        "%cd /content/naifu\n",
        "!pip install virtualenv && bash ./setup.sh\n",
        "!curl -Ls https://github.com/ekzhang/bore/releases/download/v0.4.0/bore-v0.4.0-x86_64-unknown-linux-musl.tar.gz | tar zx -C /usr/bin\n",
        "!curl -Lo /usr/bin/cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 && chmod +x /usr/bin/cloudflared"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uQBR9zXQGJrn"
      },
      "outputs": [],
      "source": [
        "#@title ### 3. 啟動模型\n",
        "#@markdown 訪問輸出的映射地址（以 `trycloudflare.com` / `bore.pub` 結尾）即可。\n",
        "#@markdown - 請等待模型加載完成（出現`Application startup complete`字樣）再訪問\n",
        "#@markdown - cloudflare 提供的服務偶爾會出現請求超時，可換用 bore 隧道\n",
        "\n",
        "%cd /content/naifu\n",
        "!sed -i 's/# export SAVE_FILES=\"1\"/export SAVE_FILES=\"1\"/g' run.sh\n",
        "!bash run.sh & cloudflared tunnel --url localhost:6969"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "B9j9thAby5_2"
      },
      "outputs": [],
      "source": [
        "#@title ### 4. （可選）使用 7G 的 `animefull-latest` 模型運行\n",
        "#@markdown 默認使用的是 4G 大小的 animefull-final-pruned 模型。如果想使用 7G 的 animefull-latest 模型，運行這個\n",
        "\n",
        "%cd /content/\n",
        "!tar xf animefull-latest.tar -C /content/naifu/models && rm animefull-latest.tar\n",
        "!sed -i 's/map_location=\"cpu\"/map_location=\"cuda\"/g' /content/naifu/hydra_node/models.py\n",
        "\n",
        "%cd /content/naifu\n",
        "%env DTYPE=float16\n",
        "%env CLIP_CONTEXTS=3\n",
        "%env AMP=1\n",
        "%env MODEL=stable-diffusion\n",
        "%env DEV=True\n",
        "%env MODEL_PATH=models/animefull-latest\n",
        "%env ENABLE_EMA=1\n",
        "%env VAE_PATH=models/animevae.pt\n",
        "%env PENULTIMATE=1\n",
        "%env PYTHONDONTWRITEBYTECODE=1\n",
        "%env SAVE_FILES=1\n",
        "\n",
        "!./venv/bin/python -m uvicorn --host 0.0.0.0 --port=6969 main:app & bore local 6969 --to bore.pub & cloudflared tunnel --url localhost:6969"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
